package cn.niit.config;


import cn.niit.interceptor.MenuInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
public class WebMVCConfig implements WebMvcConfigurer {
    /**
     * 自动注入
     */

    @Autowired
    private MenuInterceptor menuInterceptor;

    @Value("${springmvc.interceptor.exclude-path-patterns}")
    private List<String> EXCLUDE_PATH_PATTERNS;

    /**
     * 注册拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        System.out.println("EXCLUDE_PATH_PATTERNS = " + EXCLUDE_PATH_PATTERNS);

        InterceptorRegistration registration = registry.addInterceptor(menuInterceptor);
        registration.addPathPatterns("/**");    //拦截所有请求

//        *
//         * 设置拦截器白名单：将以下请求排除在拦截目录范围内
        registration.excludePathPatterns("/login");
        registration.excludePathPatterns("/css/**", "/calendar/**", "/images/**", "/js/**","/user/register");
        registration.excludePathPatterns(EXCLUDE_PATH_PATTERNS);
    }

    /**
     * 实现跨域请求配置
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        /** 对哪些请求路径进行跨域处理 */
        registry.addMapping("/api/**")
                /** 允许的请求头，默认允许所有的请求头 */
                .allowedHeaders("*")
                /** 允许的请求方法：默认允许GET、POST、HEAD */
                .allowedMethods("*")
                /** 探测请求有效时间，单位：秒 */
                .maxAge(1800)
                /** 支持的域 */
                //.allowedOriginPatterns("*");
                .allowedOrigins("http://127.0.0.1:82");
    }
}
